import 'package:flutter/material.dart';
import 'package:sy_flutter_alipay/sy_flutter_alipay.dart';
// import 'package:sy_flutter_wechat/sy_flutter_wechat.dart';

import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';

class AliPay extends StatefulWidget {
  AliPay({Key key}) : super(key: key);

  @override
  _AliPayState createState() => _AliPayState();
}

class _AliPayState extends State<AliPay> {

  var _groupValue = 1;

  _doPay(){
    if(this._groupValue == 1) {
      this._doAliPay();
    } else {
      this._doWxPay();
    }
  }

  //执行支付宝支付
  _doAliPay() async {
    //http://agent.itying.com/alipay/
    var serverApi = 'http://140.143.203.80/maque/pay/alipayOrder';
    var serverData = await Dio().get(serverApi);
    var payInfo = serverData.data; //签名后的订单信息

    print("----------------签名后的订单信息----------------");
    print(payInfo['result']);

    var result = await SyFlutterAlipay.pay(
      payInfo['result'],
      // urlScheme: '你的ios urlScheme', //前面配置的urlScheme
      isSandbox: true //是否是沙箱环境，只对android有效
    );

    print("----------------支付完成后返回的信息----------------");
    print(result);

  }

  // 执行微信支付
  _doWxPay() {
    Fluttertoast.showToast(
        msg: "抱歉，未接入微信支付！",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.grey[600],
        textColor: Colors.white,
        fontSize: 16.0
    );

    /*
    * 1.请求后台取订单信息,后台请求微信统一下单接口
    * 2.app请求微信服务器
    * 3.微信给服务器回调通知
    * 4.微信给移动端返回结果
    */

    // String payInfo =
    //                 '{"appid":"wxf9909bde17439ac2","partnerid":"1518469211","prepayid":"wx120649521695951d501636f91748325073","package":"Sign=WXPay","noncestr":"1541976592","timestamp":"1541976592","sign":"E760C99A1A981B9A7D8F17B08EF60FCC"}';
    // SyPayResult payResult = await SyFlutterWechat.pay(
    //     SyPayInfo.fromJson(json.decode(payInfo)));
    // print(payResult);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('支付订单'),
        ),
        body: Container(
          width: double.infinity,
          color: Color.fromRGBO(244, 244, 244, 1),
          child: Column(
            children: <Widget>[
              SizedBox(height: 100),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    child: Text('￥99.70', textAlign: TextAlign.center, style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold, color: Color.fromRGBO(17, 17, 17, 1)))
                  )
                ], 
              ),
              Row(
                children: <Widget>[
                  Expanded(
                    child: Text('100元话费充值-18278377807', textAlign: TextAlign.center, style: TextStyle(fontSize: 12, color: Color.fromRGBO(106, 106, 106, 1)))
                  )
                ], 
              ),
              SizedBox(height: 100),
              Padding(
                padding: EdgeInsets.fromLTRB(20, 20, 20, 0),
                child: RadioListTile(
                  title: Text('微信支付'),
                  secondary: Image.asset('images/wxpay.png', width: 40, height: 40),
                  value: 0,
                  groupValue: _groupValue, 
                  selected: _groupValue==0,
                  onChanged: _handleChange
                ),
              ),
              Padding(
                padding: EdgeInsets.fromLTRB(20, 0, 20, 20),
                child: RadioListTile(
                  title: Text('支付宝支付'),
                  secondary: Image.asset('images/alipay.png', width: 40, height: 40),
                  value: 1, 
                  groupValue: _groupValue,
                  selected: _groupValue==1,
                  onChanged: _handleChange
                )
              ),
              SizedBox(height: 100),

              Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                      height: 50,
                      margin: EdgeInsets.all(20),
                      child: RaisedButton(
                        child: Text('确认支付',style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18)), 
                        onPressed: _doPay,
                        color: Colors.blue[600],
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(10) 
                        )
                      ),
                      
                    )
                  )
                ],
              ),
                
              
            ], 
          ) 
        )
      );
  }

  _handleChange(val) {
    setState(() {
      this._groupValue = val;
    });
  }
}
